package com.upcode.weighing.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.upcode.weighing.base.controller.BaseController;
import com.upcode.weighing.domain.SysDict;
import com.upcode.weighing.service.SysDictService;

import com.upcode.weighing.vo.resp.BaseResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.constraints.NotNull;
import java.util.List;



/**
 * @author liuxinghong
 * @Description:
 * @date 2018/5/4/00415:47
 */
@RestController
@RequestMapping(value = "/dict")
@Api(value = "/dictType",description = "字典接口",produces = MediaType.ALL_VALUE,tags = {"字典接口"})
public class DictController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(DictController.class);

    @Autowired
    private SysDictService sysDictService;



    @ApiOperation(value = "根据字典类型查询字典",notes ="")
    @GetMapping(value = "/list/{typeId}")
    public ResponseEntity<BaseResponseVo<List<SysDict>>> list(@PathVariable("typeId") @NotNull(message = "字典类型ID不能为空") Long id){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("type_id", id);
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("sort_num");
//        return success(sysDictService.list(ImmutableMap.of("type_id", id ,"status",1)));
        List<SysDict> list=sysDictService.list(queryWrapper);
        return HttpResponse.success(list);
    }
//    @ApiOperation(value = "新增字典",notes ="")
//    @PostMapping(value = "/add")
//    public Object add(@Validated @RequestBody SysDictAddVo vo){
//        boolean insert = sysDictService.add(vo);
//        if (insert){
//            return success("新增字典成功");
//        }
//        return error("新增字典失败");
//    }
//
//    @ApiOperation(value = "修改字典",notes ="")
//    @PostMapping(value = "/edit")
//    public Object edit(@Validated @RequestBody SysDictEditVo vo){
//        boolean b = sysDictService(vo);
//        if (b){
//            return success("修改字典成功");
//        }
//        return error("修改字典失败");
//    }

    @ApiOperation(value = "删除字典",notes ="")
    @PostMapping(value = "/del")
    public Object del(Long id){
        boolean b = sysDictService.removeById(id);
        if (b){
            return success("删除典成功");
        }
        return error("删除字典失败");
    }

//    @ApiOperation(value = "字典类型分页列表",notes ="")
//    @PostMapping(value = "/list")
//    public Object list(@Validated @RequestBody Page<SysDictListVo> vo) {
//        Page<SysDictVo> list = sysDictService.list(vo);
//        return success(list);
//    }

//    @ApiOperation(value = "验证同一字典类型下的字典值唯一性",notes ="")
//    @PostMapping(value = "/checkOnly")
//    public Object checkOnly(@Validated @RequestBody SysDictCheckVo vo) {
//        boolean b = sysDictService.checkOnly(vo.getId(),vo.getTypeId(),vo.getValue());
//        if(b){
//            return error("字典值已存在");
//        }
//        return success("字典值可用");
//    }

    @ApiOperation(value = "字典详情",notes ="")
    @GetMapping(value = "/details/{id}")
    public ResponseEntity<BaseResponseVo<SysDict>> details(@PathVariable("id") @NotNull(message = "字典类型ID不能为空") Long id){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id", id);
        final SysDict sysDict = sysDictService.getById(id);
        return HttpResponse.success(sysDict);
    }




}
